package com.codrim.cs.data.server.dao.mapper;

import com.codrim.cs.data.server.dao.entity.StatCsAffAllDO;
import com.codrim.cs.data.server.dao.entity.unique.key.StatCsAffAllUniqueKey;
import com.codrim.cs.data.server.dao.param.SDateAndCompanyId;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * Created by Liang.Zhuge on 14/07/2017.
 */
@Mapper
public interface StatCsAffAllMapper extends StatCsMapper<StatCsAffAllDO, StatCsAffAllUniqueKey> {
    String tableName = "e0000_day_stat_cs_aff_all";

    @Select("select " +
            "sdate, " +
            "symbol, " +
            "affiliate_id affiliateId, " +
            "click, " +
            "uclick, " +
            "conversion, " +
            "pprice " +
            "from " + tableName +
            " where sdate = #{param.sdate} and affiliate_id = #{param.companyId}")
    List<StatCsAffAllDO> findBySDateAndAffId(@Param("param") SDateAndCompanyId param,
                                             @Param("sZone") String zone);

    @Insert("insert into " + tableName + " (" +
            "sdate, symbol, affiliate_id, click, " +
            "uclick, conversion, pprice" +
            ") values (" +
            "#{param.sdate}, #{param.symbol}, #{param.affiliateId}, #{param.click}, " +
            "#{param.uclick}, #{param.conversion}, #{param.pprice}" +
            ")")
    void insert(@Param("param") StatCsAffAllDO dayStatCsAffAll, @Param("sZone") String zone, @Param("timeDimesion") String timeDimesion);

    @Delete("delete from " + tableName + " where sdate = ${sdate}")
    void deleteSpecifiedDayReportData(@Param("sdate") int sdate, @Param("sZone") String zone);

    @Override
    @Select("<script>select " +
            "id, " +
            "sdate, " +
            "symbol, " +
            "affiliate_id affiliateId, " +
            "click, " +
            "uclick, " +
            "conversion, " +
            "pprice " +
            "from " + tableName +
            " where sdate = #{param.sdate} " +
            "<choose>" +
            "<when test='param.affiliateId !=null'> and affiliate_id = #{param.affiliateId} </when>" +
            "<otherwise> and affiliate_id is null </otherwise>" +
            "</choose>" +
            "<choose>" +
            "<when test='param.symbol !=null'> and symbol = #{param.symbol} </when>" +
            "<otherwise> and symbol is null </otherwise>" +
            "</choose> limit 1 </script>")
    StatCsAffAllDO findByKey(@Param("param") StatCsAffAllUniqueKey key,
            @Param("sZone") String zone, @Param("timeDimesion") String timeDimesion);

    @Override
    @Update("update " + tableName + " set " +
            "click = #{values.click}, " +
            "uclick = #{values.uclick}, " +
            "conversion = #{values.conversion}, " +
            "pprice = #{values.pprice} " +
            "where id = #{id}")
    void updateById(@Param("values") StatCsAffAllDO dayStatCsAffAllDO,
            @Param("id") Integer id,
            @Param("sZone") String zone, @Param("timeDimesion") String timeDimesion);
}
